package top.sehnsucht;

import top.sehnsucht.dao.ArticleDao;
import top.sehnsucht.dao.impl.ArticleDaoImpl;
import top.sehnsucht.entity.Article;
import top.sehnsucht.service.ArticleService;
import top.sehnsucht.service.impl.ArticleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @Description:
 * @Author: Cai
 * @CreateTime: 2021/12/23
 */

public class UpdateArticleServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String title = req.getParameter("title");
        String date = req.getParameter("pubTime");
        String content = req.getParameter("content");
        System.out.println("update");
        Article article;
        try {
            article = new Article(id,null,title,content,new SimpleDateFormat("yyyy-MM-dd").parse(date));
            ArticleDao articleDao = new ArticleDaoImpl();
            ArticleService articleService = new ArticleServiceImpl(articleDao);
            articleService.updateArticle(article);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
